package com.one.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.material.entity.Material;
import com.one.material.mapper.MaterialMapper;
import com.one.material.service.IMaterialService;
import com.one.material.struct.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liZhenHua
 * @since 2021-12-08
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Override
    public PageResult<Material> getMaterialPageAll(Material material, Integer pageNum, Integer pageSize) {
        // 分页
        Page<Material> materialPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Material> wrapper = new QueryWrapper<>();
        // 判断是否有条件
        if (!ObjectUtils.isEmpty(material)) {
            // id
            if (!StringUtils.isEmpty(material.getId())) {
                wrapper.eq("id", material.getId());
            }
            //物料编号
            if (!StringUtils.isEmpty(material.getMaterialCode())) {
                wrapper.like("material_code", material.getMaterialCode());
            }
            //名称
            if (!StringUtils.isEmpty(material.getName())) {
                wrapper.like("name", material.getName());
            }
            //状态
            if (!StringUtils.isEmpty(material.getStatus())) {
                wrapper.eq("status", material.getStatus());
            }
            //时间
            if (material.getTime()!=null){
                wrapper.between("update_time",material.getTime()[0],material.getTime()[1]);
            }

        }
        //获取条件查询
        Page<Material> page = page(materialPage, wrapper);
        return PageResult.of(page.getTotal(),page.getRecords());
    }
}
